载入一些工具
import pandas as pd
import requests
import plotly
from plotly.offline import init_notebook_mode, iplot
init_notebook_mode()
import plotly.graph_objs as go
import datetime
导入股价数据
TSLA=pd.read_csv("TSLA.csv",index_col=0)
NIO=pd.read_csv("NIO.csv",index_col=0)
TSLA.rename(index=pd.to_datetime)
TSLA.name='特斯拉股价'
NIO.rename(index=pd.to_datetime)
NIO.name='蔚来汽车股价'
**拖动下方条状时间轴可控制时间范围,可选择蜡烛图或close-high-low两种不同方式展现数据,鼠标滑动可以查看具体数额,也可以在图中直接拖拉**
trace_plot(TSLA)
**拖动下方条状时间轴可控制时间范围,可选择蜡烛图或close-high-low两种不同方式展现数据,鼠标滑动可以查看具体数额,也可以在图中直接拖拉**
trace_plot(NIO)
—————————————————————————end———————————————————————————-
#以下为构图代码
def trace_plot(df):
trace = go.Candlestick(x=df.index,
open=df.Open,
high=df.High,
low=df.Low,
close=df.Close,
name = 'Candlestick')
trace_close = go.Scatter(x=list(df.index),
y=list(df.Close),
name='Close',line=dict(color='#71adf5'))
trace_high = go.Scatter(x=list(df.index),
y=list(df.High),
visible = False,
name='High',
line=dict(color='#f2c270'))
trace_low = go.Scatter(x=list(df.index),
y=list(df.Low),
visible = False,
name='Low',
line=dict(color='#f28170'))
data=[trace,trace_close,trace_high,trace_low]
updatemenus = list([
dict(type="buttons",
active=99,
x = 0.05,
y = 0.99,
bgcolor = '#a7bdde',
bordercolor = '#FFFFFF',
font = dict( color='#7d8ca3', size=11 ),
direction = 'left',
xanchor = 'left',
yanchor = 'top',
buttons=list([
dict(label = 'Candlestick',
method = 'update',
args = [{'visible': [True, False, False, False]},
{'title': f'Candlestick of this ticker {df.name}'}]),
dict(label = 'Close-High-Low',
method = 'update',
args = [{'visible': [False, True, True, True]},
{'title': f'Close, High,and Low price of this ticker {df.name}'}
])
]))])
# define the data and layout, and store them in the fig dictionary
layout=go.Layout(title=f"{df.name}",autosize=True,updatemenus=updatemenus,
plot_bgcolor = '#ffe5e8')
#I had to use offline.iplot in order to show this graph in notebook
fig=dict(data=data,layout=layout)
return plotly.offline.iplot(fig)